package com.binguo.api.transfer;

import com.binguo.api.form.OrderForm;
import com.binguo.api.form.OrderItemForm;
import com.binguo.model.api.dto.OrderDTO;
import com.binguo.model.api.dto.OrderItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * Created by haibang on 18/5/10.
 */
@Component
public class OrderFormTransfer {

    @Autowired
    private OrderNoBuilder orderNoBuilder;

    public OrderDTO toOrderDTO(OrderForm form){
        List<OrderItemDTO> orderItemDTOs = toOrderItemDTOs(form.getItems());
//        String orderNo = UUID.randomUUID().toString().replace("-", "");
        String orderNo = orderNoBuilder.createPlatformOrderNo();
        return OrderDTO.builder()
                .orderNo(orderNo)
                .serialNumber(form.getSerialNumber())
                .shopId(form.getShopId())
                .cashierDeskId(form.getCashierDeskId())
                .openId(form.getOpenId())
                .customerId(form.getCustomerId())
                .customerName(form.getCustomerName())
                .customerPhone(form.getCustomerPhone())
                .cardNo(form.getCardNo())
                .accountBalance(form.getAccountBalance())
                .authCode(form.getAuthCode())
                .payType(form.getPayType())
                .totalFee(form.getTotalFee())
                .realFee(form.getRealFee())
                .isSMKOrder(form.getIsSMKOrder())
                .packageId(form.getPackageId())
                .totalQuantity(form.getTotalQuantity())
                .productOrderNo(form.getProductOrderNo())
                .userId(form.getUserId())
                .posseq(form.getPosseq())
                .icdata(form.getIcdata())
                .items(orderItemDTOs).build();

    }


    public List<OrderItemDTO> toOrderItemDTOs(List<OrderItemForm> list){
      return list.stream().map(orderItemForm ->
              OrderItemDTO.builder()
                      .productId(orderItemForm.getProductId())
                      .price(orderItemForm.getPrice())
                      .seq(orderItemForm.getSeq())
                      .change(orderItemForm.getChange())
                      .searchId(orderItemForm.getSearchId())
                      .quantity(orderItemForm.getQuantity()).build()
      ).collect(Collectors.toList());
    }

}
